/**
 * 项目名称：MSH
 * 包名称：com.mt.sh.test.init
 * 文件名称：InItData.java
 */
package com.mt.sh.test.init;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.dom4j.Document;
import org.dom4j.Element;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.mt.component.consts.PermissionName;
import com.mt.component.consts.SysConst;
import com.mt.system.domain.Funct;
import com.mt.system.domain.Menu;
import com.mt.system.domain.Permission;
import com.mt.system.domain.Role;
import com.mt.system.domain.User;
import com.mt.system.service.FunctionService;
import com.mt.system.service.MenuService;
import com.mt.system.service.PermissionService;
import com.mt.system.service.RoleService;
import com.mt.system.service.UserService;
import com.mt.utils.XmlUtils;


/**
 * 类名称：InItData <br>
 * 类描述：初始化系统数据 <br>
 * 创建人： 马腾 <br>
 * 创建时间：Oct 18, 2012 1:07:13 PM <br>
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext.xml" })
public class InItCommon {
	
	@Resource private UserService userService;
	@Resource private RoleService roleService;
	@Resource private FunctionService functionService;
	@Resource private MenuService menuService;
	@Resource private PermissionService permissionService;
	
	private String permissionNames = "";
	
	@Test
	public void initFunction(){
		File file = new File("data/function.csv");
		BufferedReader bufferedReader = null;
		try {
			bufferedReader = new BufferedReader(new FileReader(file));
			String a = "";
			while((a = bufferedReader.readLine()) != null){
				Funct funct = new Funct();
				funct.setName(a);
				functionService.addFunction(funct);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if(bufferedReader != null){
				try {
					bufferedReader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	@SuppressWarnings("unchecked")
	@Test
	public void initPermission(){
		Document document = XmlUtils.readXml("data/permission.xml");
		Element root = document.getRootElement();
		Iterator<Element> iterator1 = root.elementIterator();
		while(iterator1.hasNext()){
			Element element1 = iterator1.next();
			Menu menu1 = addMenu(element1,1);
			Permission per1 = addPermission(menu1,null,null,Integer.parseInt(element1.attributeValue("sn")),element1.attributeValue("mark"));
			Iterator<Element> iterator2 = element1.elementIterator();
			while(iterator2.hasNext()){
				Element element2 = iterator2.next();
				Menu menu2 = addMenu(element2,2);
				Permission per2 = addPermission(menu2,null,per1,Integer.parseInt(element2.attributeValue("sn")),element2.attributeValue("mark"));
				Iterator<Element> iterator3 = element2.elementIterator();
				while(iterator3.hasNext()){
					Element element3 = iterator3.next();
					Funct funct = functionService.getFunctionByName(element3.attributeValue("name"));
					addPermission(menu2,funct,per2,Integer.parseInt(element3.attributeValue("sn")),element3.attributeValue("mark"));
				}
			}
		}
		System.out.println();
		System.out.println(permissionNames);
	}

	@Test
	public void initRole(){
		List<Permission> allPermissions = permissionService.getPermissions();
		List<Permission> commonPermissions = new ArrayList<Permission>();
		List<Permission> operatPermissions = new ArrayList<Permission>();
		for (Permission permission : allPermissions) {
			if(permission.getMark().indexOf("ROLE") == -1 && permission.getMark().indexOf("USER") == -1){
				operatPermissions.add(permission);
			}
			if(permission.getMark().equals(PermissionName.USER_MGR) || permission.getMark().indexOf("ROLE") == -1) {
				commonPermissions.add(permission);
			}
		}
		Role role = new Role();
		role.setName("admin");
		role.setDesc("超级管理员");
		role.setPermissions(allPermissions);
		role.setOperateTime(new Date());
		roleService.addRole(role);
		
		Role role2 = new Role();
		role2.setName("common");
		role2.setDesc("普通管理员");
		role2.setPermissions(commonPermissions);
		role2.setOperateTime(new Date());
		roleService.addRole(role2);
		
		Role role3 = new Role();
		role3.setName("root");
		role3.setDesc("管理员");
		role3.setPermissions(allPermissions);
		role3.setOperateTime(new Date());
		roleService.addRole(role3);
		
		Role role4 = new Role();
		role4.setName("operator");
		role4.setDesc("操作员");
		role4.setPermissions(operatPermissions);
		role4.setOperateTime(new Date());
		roleService.addRole(role4);
	}
	
	@Test
	public void initUser(){
		String[] names = new String[]{"admin","operator" , "common" , "root"};
		for (String name : names) {
			User user = new User();
			user.setName(name);
			user.setPassword("123456");
			user.setEmail(name + "@163.com");
			user.setPhone("12345678901");
			user.setOperateTime(new Date());
			List<Role> roles = new java.util.ArrayList<Role>();
			roles.add(roleService.getRoleByName(name));
			user.setRoles(roles);
			user.setSex(SysConst.SEX_MALE);
			userService.addUser(user);
		}
	}
	
	private Permission addPermission(Menu menu , Funct funct , Permission parent , int sn , String mark) {
		Permission per = new Permission();
		if(funct == null){
			per.setDesc(menu.getName());
		}else{
			per.setDesc(menu.getName() + "-" + funct.getName());
		}
		per.setFunct(funct);
		per.setMark(mark);
		per.setMenu(menu);
		per.setParent(parent);
		per.setSn(sn);
		System.out.println("/**" + per.getDesc() + "*/");
		System.out.println("public static final String " + mark + " = \"" + mark + "\";");
		permissionNames += "/**" + per.getDesc() + "*/\n";
		permissionNames += "public static const " + mark + ":String = \"" + mark + "\";\n";
		return permissionService.addPermission(per);
	}

	private Menu addMenu(Element element , int level){
		Menu menu = new Menu();
		menu.setName(element.attributeValue("name"));
		menu.setIcon(element.attributeValue("icon"));
		menu.setLevel(level);
		menu.setAction(element.attributeValue("action"));
		menu.setSn(Integer.parseInt(element.attributeValue("sn")));
		return menuService.addMenu(menu);
	}
}
